<?php
/**
 *
 * @version 2.0.0
 * @package epl
 * @author  Evren Ceyhan <ceyceyone@live.com>
 */
namespace epl;

use epl\io\IOException;
use epl\io\FileNotFoundException;
use epl\core\ClassNotFoundException;


################################################################################
## Global Constants
################################################################################
// The current timestamp and memory usage when EPL was initialized.
\define('EPL_INIT', \microtime(true) . '|' . \memory_get_usage());

// The root path is which is mandatory for EPL to load resources.
\define('EPL_ROOT', \dirname(\dirname(__FILE__)) .'/');

// Debug true means development otherwise production mode.
\define('EPL_DEBUG', true);

// The EPL version of this release.
\define('EPL_VERSION', '2.0.0 beta');


################################################################################
## Common Functions
################################################################################
/**
 * Converts back slashed namespaces into dotted or slashed notation.
 * 
 * This function can be used for:
 *  - fixing back slashed PHP notation into dotted JAVA-like notation in 
 *    the specified namespace to prevent confusing between paths and namespaces.
 * 
 *  - converting backslashes into slashes including the root folder to ensure
 *    file system compatibility on different platforms such as Windows, Unix etc.
 * 
 * @param string $namespace
 * @param boolean $pathNotation
 * @return string 
 */
function fixNotation($namespace, $pathNotation = true)
{
    if($pathNotation) {
        $namespace = \EPL_ROOT . $namespace;
    }
    return \str_replace('\\', $pathNotation ? '/' : '.', $namespace);
}

/**
 * 
 *
 * @param string $filename
 * @param string $ext
 * @return mixed
 * @throws FileNotFoundException
 * @throws IOException
 */
function load($filename, $ext = '.php')
{
    // set the full path of given filename
    $filename = fixNotation($filename . $ext);

    if(!\file_exists($filename)) {
        throw new FileNotFoundException($filename);
    }
    else if(!\is_readable($filename)) {
        throw new IOException('Cannot read from the file: %s', $filename);
    }
    return require $filename;
}


################################################################################
## Common Initializations
################################################################################
/**
 *
 * @param $e
 */
\set_exception_handler(function(\Exception $e)
{
    echo $e;
});

/**
 * Catches all non-fatal errors and converts them into ExceptionError
 * which will be cought by exception handler to display or hide them from end user.
 *
 * @param $code
 * @param $message
 * @param $filename
 * @param $line
 * @param $context
 * @return void
 * @throws ErrorException
 */
\set_error_handler(function($code, $message, $filename, $line, $context)
{
    throw new \ErrorException($message, $code, null, $filename, $line);
});

/**
 * Loads classes automatically based on their namespaces if any present.
 *
 * @param $className
 * @return mixed
 * @throws ClassNotFoundException
 */
\spl_autoload_register(function($className)
{
    try {
        load($className);
    }
    catch(\Exception $e) {
        throw new ClassNotFoundException($className, $e);
    }
});

/**
 * 
 * @param string $buffer
 * @return string
 */
\ob_start(function($buffer)
{
    $errors = \error_get_last();
    if(isset($errors)) {
        $buffer = '';
        foreach($errors as $info => $str) {
            $buffer .= $info .' - '. $str;
        }
    }
    return $buffer;
});


new abc();
